package cn.com.bluemoon.daps.standard.common;

import cn.com.bluemoon.daps.standard.entity.DapStandardVersion;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.gson.JsonSyntaxException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.time.LocalDateTime;

import static cn.com.bluemoon.daps.standard.service.impl.DapStandardAuditVersionMainServiceImpl.CUSTOM_GSON;

/**
 * @author Jarod.Kong
 */
@Data
@NoArgsConstructor
public abstract class StandardVersionResponse<T extends Serializable> {

    @ApiModelProperty("版本id")
    private String versionId;

    @ApiModelProperty("版本号 eg: V1 V2")
    private String version;

    @ApiModelProperty(value = "变更时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime modifyTime;

    @ApiModelProperty(value = "变更类型：新增（1） 变更(2) 删除(3)")
    private Integer modifyType;

    @ApiModelProperty(value = "标准唯一id")
    private String standardUniqueId;

    @ApiModelProperty(value = "版本内容（对象结构）")
    private T modifyContentBean;

    @ApiModelProperty(value = "变更说明")
    private String modifyDesc;

    @ApiModelProperty(value = "标准类型 0基础标准 1指标标准 默认为1")
    private Integer standardType;

    @ApiModelProperty(value = "审批流程-通过的流程id")
    private String auditProcessId;

    public StandardVersionResponse(DapStandardVersion version) {
        BeanUtils.copyProperties(version, this);
        this.versionId = version.getId();
        if (version.getStandardJson() != null) {
            try {
                this.modifyContentBean = CUSTOM_GSON.fromJson(version.getStandardJson(), modifyContentBean());
            } catch (JsonSyntaxException e) {
                e.printStackTrace();
                this.modifyContentBean = null;
            }
        }
    }

    protected abstract Class<T> modifyContentBean();
}
